import random
import pymysql

class DatabaseUtil:
    @staticmethod
    def get_random_ids():
        '''
        pymysql 数据库连接，查询需要字段操作
        '''
        connection = pymysql.connect(
            db='api_pro',
            user='root',
            password='r00t123',
            host='192.168.***.***',
            port=3308
        )

        cursor = connection.cursor()

        # 设备模板ID
        cursor.execute("SELECT id FROM `api_pro`.`device_temp` where is_del = 0 ")
        temp_id = cursor.fetchall()

        # 数据中心ID
        cursor.execute("SELECT id FROM `api_pro`.`data_center` ORDER BY `name` DESC LIMIT 0,1000")
        dc_id = cursor.fetchall()

        # 随机选择一个
        random_temp_id = random.choice(temp_id)[0]
        random_dc_id = random.choice(dc_id)[0]

        # 关闭数据库连接
        cursor.close()
        connection.close()

        return {'temp_id': random_temp_id, 'dc_id': random_dc_id}
